$(function(){
	/* 缓存输入内容 */
    var storage = window.localStorage;
	//机器人动画
    $(".robot-robotBodyImg").mouseleave(function(){
        $(".robot-robotBodyImg").addClass("robotLeave");
    })
    function bubbleBigShow(){
        var bubbleBig = $('.bubble-big');
        var bubbleMiddle = $('.bubble-middle');
        var bubbleSmall = $('.bubble-small');
        setInterval(function() {
            var isShow = bubbleBig.is(':visible');
            bubbleBig[isShow ? 'fadeOut' : 'fadeIn']('fadeIn');
        }, 3000);
        setInterval(function() {
            var isShow = bubbleMiddle.is(':visible');
            bubbleMiddle[isShow ? 'fadeOut' : 'fadeIn']('fadeIn');
        }, 2500);
        setInterval(function() {
            var isShow = bubbleSmall.is(':visible');
            bubbleSmall[isShow ? 'fadeOut' : 'fadeIn']('fadeIn');
        }, 2000);
    }
    bubbleBigShow();
    var url = window.location.href;
	var _chatBody = $(".chat-body");
	/*回车键调用事件*/
	$(document).on("keydown",".chat-send",function(e){
        if(ev.keyCode==13) {
        	chatSendMet();
        }
    });
	/*关闭按钮*/
    $("#robot-close").click(function(){
        $(".chat-content").hide("normal");
        $(".robot-content").show(2000);
    });
    /*点击泡泡弹出*/
    $(".robot-right").click(function(){
        $(".robot-content").hide();
        $(".chat-content").show("normal");
    });
    /*输入方法*/
    $(".btn-send").click(function() {
        chatSendMet();
        $('.chat-body').scrollTop($('.chat-body')[0].scrollHeight);
    })
    /*输入方法*/
    var question = [];
    /* 回显输入内容 */
    if(storage.getItem("robotSession") != null && storage.getItem("robotSession") != "undefined"){
    	_chatBody.append(storage.getItem("robotSession"));
    }
    /* 用户输入 */
    function chatSendMet(){
    	/*输入内容*/
    	var _chatSend = $(".chat-send").val();
    	if(_chatSend == '' || _chatSend == 'undefined'){
    		return;
    	}
    	var _robotHtml = '';
    		_robotHtml  += '<div class="chatCustomer">'
    		_robotHtml  += '<div class="txtCustomerBox">'
			_robotHtml  += '<p>'+_chatSend+'</p>'
			_robotHtml  += '</div></div>'
			_chatBody.append(_robotHtml);
    		_robotHtml = "";
		if(question.length > 0){
			_robotHtml  += '<div class="chatSelf">'
			_robotHtml  += '<div class="txtSelfBox">';
			_robotHtml  += '<p>';
			if(question[_chatSend-1] != null){
				console.log(question[_chatSend-1]);
				_robotHtml  += question[_chatSend-1];
				_robotHtml  += '</p></div></div>';
				_chatBody.append(_robotHtml);
				$(".chat-send").val('');
		    	storage.setItem("robotSession",_chatBody.html());
				return;
			}
		}
		$.ajax({
			type : 'post',
			url : basepath+"/mquestion/getQuestion4Participles",
			data : {"chatSend" : _chatSend},
			dataType : 'json',
			async : false,
			success : function(data) {
				_robotHtml = "";
				_robotHtml  += '<div class="chatSelf">'
				_robotHtml  += '<div class="txtSelfBox">';
				if (data.mqs.length) {
					if(data.mqs.length == 1){
						$.each(data.mqs,function(index,value) {
							_robotHtml  += '<p>'+ value.answer+'</p>';
						});
					}else{
						_robotHtml  += '<p>你想问的是:</p>';
						question = [];
						$.each(data.mqs,function(index,value) {
							_robotHtml  += '<p>'+ (index + 1) +'、'+value.question+'</p>';
							question.push(value.answer);
						});
					}
				}else{
					_robotHtml  += '<p>未查询出结果哦~,也许你该换种方式和小m沟通，如菜单名称模糊查询。</p>';
				}
				_robotHtml  += '</div></div>';
				_chatBody.append(_robotHtml);
                $('.chat-body').scrollTop($('.chat-body')[0].scrollHeight);
			},
			error:function(err){
				var _robotHtml   = '<div class="chatSelf">'
					_robotHtml  += '<div class="txtSelfBox">';
					_robotHtml  += '<p>未查询出结果哦~,也许你该换种方式和小m沟通，如菜单名称模糊查询。</p>';
					_robotHtml  += '</div></div>';
				_chatBody.append(_robotHtml);
			}
		});
    	/*清空输入内容*/
    	$(".chat-send").val('');
    	var chatBody = _chatBody.html();
    	storage.setItem("robotSession",chatBody);
    };
    if(storage.getItem("refresh") =='yes'){
    	$.ajax({
    		type : 'post',
    		url : basepath+"/robot/getMis4CaseIdAndUserId",
    		data : {"requestUrl" : url},
    		dataType : 'json',
    		async : false,
    		success : function(data) {
    			var html = '';
    			/**查询代办账号信息**/
    			if(data.agencys){
    				html  += '<p class="chat-body-do">有以下账单未导入:</p>';
    				$.each(data.agencys,function(index,value) {
    					var bank = value.ACCOUNT_RIVAL_BANK == null ? '' : value.ACCOUNT_RIVAL_BANK;
    					var accountbody = value.ACCOUNT_RIVAL == null ? '' : value.ACCOUNT_RIVAL;
    					html  += '<p>'+bank+' '+accountbody+' '+value.ACCOUNT_RIVAL_CARD+'</p>';
    				});
    			}
    			_chatBody.append(html);
    			storage.setItem("refresh","no")
    		},
    		error:function(err){
    		}
    	});
    }
})